-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
suggestions #7382
suggestions #7382
Conversation
@@ -16,9 +16,9 @@ export * as profile from '#/types/atproto/profile' | |||
* } | |||
* ``` | |||
*/ | |||
export function fastIsType<R>( | |||
export function fastIsType<R extends {$type?: string}>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change prevents using fastIsType
with a type param R
that does not match the predicate function.
// Type casting here is actually safe because here we check for the | ||
// actual type bellow | ||
const record = notif.record as | ||
| AppBskyFeedRepost.Record | ||
| AppBskyFeedLike.Record |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally! This is actually what fastIsType
is doing as well, just casting the values without validation. I decided to make the util so that it's clear what's happening when that util is used, instead of feeling the need to leave a comment on each type cast as you did here. That seem reasonable to you?
export function fastIsType<R extends {$type?: string}>( | ||
record: unknown, | ||
identity: <V>(v: V) => boolean, | ||
identity: <V>(v: V) => v is V & {$type: NonNullable<R['$type']>}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh v nice!
|
src/components/MediaPreview.tsx
Outdated
embed?: | ||
| $Typed<AppBskyEmbedRecordWithMedia.View> | ||
| $Typed<AppBskyEmbedImages.View> | ||
| $Typed<AppBskyEmbedVideo.View> | ||
| $Typed<AppBskyEmbedExternal.View> | ||
| {$type: string} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get why this makes sense and is probably the most academic way of doing this, but a few things I don't like:
- We lose the relationship between the prop
embed
and the origin of the data inPostView['embed']
. The way it was typed before makes it very obvious how this component is used - Having to remember to duplicate the forwards compat via
{$type: string}
in components like this is boilerplate I don't think we want. If we adopt this pattern, we'll be doing this in hundreds of places. - Having to reconstruct these types exactly but understanding exactly what can be passed in here (plus needing the $Type util) requires understanding of the data on both sides of this component. This makes it more cumbersome to make "dumb" components that can be composed and reused throughout the app.
* suggestions * Revert unneeded changes --------- Co-authored-by: Eric Bailey <[email protected]>
No description provided.